using System.Collections.Generic;
using System.Linq;

namespace GameLogic.Poker
{
    public class HandComparer : IHandComparer
    {
        public int Compare(Hand hand1, Hand hand2)
        {
            var cards1 = hand1.Cards.OrderBy(x => x.Rang).ToArray();
            var cards2 = hand2.Cards.OrderBy(x => x.Rang).ToArray();

            for (var i = 0; i < 5; i++)
            {
                var comparison = cards1[i].Rang.CompareTo(cards2[i].Rang);
                if (comparison != 0)
                    return comparison;
            }
            return 0;
            //return hand1.Cards.Max(x => x.Rang).CompareTo(hand2.Cards.Max(x => x.Rang));
        }
        public Hand FindTheBestHand(IEnumerable<Hand> hands)
        {
            var bestHand = hands.First();
            HandRang rang = bestHand.HandRang;
            foreach (var hand in hands)
            {
                if (hand.HandRang > bestHand.HandRang)
                    bestHand = hand;
            }
            return bestHand;
            //return hands.OrderByDescending(x => x.HandRang).First();
        }
    }
}